package com.kai.base.utils

import java.math.BigDecimal
import java.math.RoundingMode
import java.util.regex.Matcher
import java.util.regex.Pattern


object IStringUtils {


    /**
     * 是否为空或空字符串
     */
    fun isNullOrEmpty(str: String?): Boolean {
        return null == str || "null" == str || str.trim { it <= ' ' }.isEmpty()
    }

    fun isNoEmpty(str: String?): Boolean {
        return !isNullOrEmpty(str)
    }

    /**
     * 根据分号拆分src
     */
    fun srcSplit(str: String, sign: String): Array<String> {
        return if (!isNullOrEmpty(str) && str.contains(sign)) {
            str.split(sign.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        } else arrayOf(str)
    }

    /**
     * 取得一个string字符串防止空值
     */
    fun getString(value: String?): String {
        return if (!isNullOrEmpty(value)) {
            value!!
        } else ""
    }

    /**
     * 取得一个string字符串防止空值
     */
    fun getString(value: String?, key: String): String {
        return if (!isNullOrEmpty(value)) {
            value!!
        } else key
    }



    fun replaceContent(src: String?): String {
        var dest = ""
        if (src != null) {
            val pattern = Pattern.compile("。|……")
            val matcher = pattern.matcher(src)
            dest = matcher.replaceAll("<br/><br/>")
        }
        if (dest.contains("作者")) {

        }
        return dest.trim()
    }

    fun toDouble(s: String?): Double {
        var to: Double = 0.00
        if (isNoEmpty(s)) {
            try {
                to = s!!.toDouble()
            } catch (e: Exception) {
                return to
            }
        }
        return to
    }


    fun toLong(s: String?): Long {
        var to: Long = 0
        if (isInteger(s!!)) {
            to = s!!.toLong()
        }
        return to
    }

    fun dblToLocation(data: Double): String {
        var ret_s = ""
        val tmp_i_du = data.toInt()
        ret_s = "$tmp_i_du°"
        //度小数部分
        val tmp_d_du = data - tmp_i_du
        val tmp_i_fen = (tmp_d_du * 60).toInt()
        ret_s = "$ret_s$tmp_i_fen′"
        val tmp_d_fen = tmp_d_du * 60 - tmp_i_fen
        val tmp_i_miao = (tmp_d_fen * 60).toInt()
        ret_s = "$ret_s$tmp_i_miao″"
        return ret_s
    }

    fun format1(value: Double): String {
        var bd = BigDecimal(value);
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        return bd.toString()
    }

    /*方法二：推荐，速度最快
  * 判断是否为整数
  * @param str 传入的字符串
  * @return 是整数返回true,否则返回false
*/
    fun isInteger(str: String): Boolean {
        val pattern = Pattern.compile("^[-\\+]?[\\d]*$")
        return pattern.matcher(str).matches()
    }

//    fun getCompleteUrl(url: String): String {
//        var url = url
//        if (isNullOrEmpty(url)) {
//            return ""
//        }
//        if (Patterns.WEB_URL.matcher(url).matches()) {
//            //符合标准
//            return url
//        }
//        if (url.startsWith("/")) {
//            url = url.replaceFirst("/".toRegex(), "")
//        }
//        return CommonConstant.SERVER_URL1 + url
//    }

    /**
     *判断url 是否是视频链接
     * */
    fun getISvideourl(url: String): Boolean {
        var reg = "rm|rmvb|mpeg1-4|mov|mtv|dat|wmv|avi|3gp|amv|dmv|flv|mp4"
        var p = Pattern.compile(reg)
        return p.matcher(url.toLowerCase()).find()
    }

    fun Html2Text(htmlStr: String?): String {
        if (isNullOrEmpty(htmlStr)) {
            return ""
        }
        var regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>" //定义script的正则表达式

        var regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>" //定义style的正则表达式

        var regEx_html = "<[^>]+>" //定义HTML标签的正则表达式


        var p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE)
        var m_script: Matcher = p_script.matcher(htmlStr)
        var htmlStr1 = m_script.replaceAll("") //过滤script标签


        var p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE)
        var m_style: Matcher = p_style.matcher(htmlStr1)
        htmlStr1 = m_style.replaceAll("") //过滤style标签


        var p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE)
        var m_html: Matcher = p_html.matcher(htmlStr1)
        htmlStr1 = m_html.replaceAll("") //过滤html标签


        return htmlStr1.trim() //返回文本字符串

    }

}
